package rcs34.android.AndroidRC;


import java.util.HashMap;
import java.util.Set;

import org.json.JSONException;

import android.app.IntentService;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.Bundle;

public class ServiceSender extends IntentService {
  public static final int CHANGE_BATT=1;
  public static final int CHANGE_STORAGE=2;
  public static final int CHANGE_CALL=3;
  public static final String KEY_CMD = "command";

  public ServiceSender() {
    super("Sender service");
  }

  @Override
  protected void onHandleIntent(Intent intent) {Bundle extras = intent.getExtras();
    HashMap<String, Object> map = new HashMap<String, Object>();
    int cmd = intent.getIntExtra(KEY_CMD, -1);
    switch (cmd) {
    case CHANGE_BATT:
       reportBattInfo(extras);
      break;
    case CHANGE_STORAGE:
      //reportStorageInfo();
     break;
    case CHANGE_CALL:
      map = callstateInfoMap(extras);
      break;
    }
  }

  private void reportBattInfo(Bundle extras) {
    Set<String> keys = extras.keySet();
    for (String key : keys) {
      String mapKey = null;
      int mapValue = -1;
      if (key.equals(BatteryManager.EXTRA_HEALTH)) {
        mapKey = CommDefs.KEY_BATT_HEALTH;
        switch (extras.getInt(key)) {
        case BatteryManager.BATTERY_HEALTH_COLD:
          mapValue = CommDefs.BATT_HEALTH_COLD;
          break;
        case BatteryManager.BATTERY_HEALTH_DEAD:
          mapValue = CommDefs.BATT_HEALTH_DEAD;
          break;
        case BatteryManager.BATTERY_HEALTH_GOOD:
          mapValue = CommDefs.BATT_HEALTH_GOOD;
          break;
        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
          mapValue = CommDefs.BATT_HEALTH_OVER_VOLTAGE;
          break;
        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
          mapValue = CommDefs.BATT_HEALTH_OVERHEAT;
          break;
        case BatteryManager.BATTERY_HEALTH_UNKNOWN:
          mapValue = CommDefs.BATT_HEALTH_UNKNOWN;
          break;
        case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
          mapValue = CommDefs.BATT_HEALTH_FAILURE;
          break;
        }
      } else if (key.equals(BatteryManager.EXTRA_LEVEL)) {
        mapKey = CommDefs.KEY_BATT_LEVEL;
        mapValue = extras.getInt(key);
      } else if (key.equals(BatteryManager.EXTRA_TEMPERATURE)) {
        mapKey = CommDefs.KEY_BATT_TEMPERATURE;
        mapValue = extras.getInt(key);
      } else if (key.equals(BatteryManager.EXTRA_VOLTAGE)) {
        mapKey = CommDefs.KEY_BATT_VOLTAGE;
        mapValue = extras.getInt(key);
      } else if (key.equals(BatteryManager.EXTRA_SCALE)) {
        mapKey = CommDefs.KEY_BATT_SCALE;
        mapValue = extras.getInt(key);
      } else if (key.equals(BatteryManager.EXTRA_STATUS)) {
        mapKey = CommDefs.KEY_BATT_STATUS;
        switch (extras.getInt(key)) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
          mapValue = CommDefs.BATT_STATUS_CHARGING;
          break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
          mapValue = CommDefs.BATT_STATUS_DISCHARGING;
          break;
        case BatteryManager.BATTERY_STATUS_FULL:
          mapValue = CommDefs.BATT_STATUS_FULL;
          break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
          mapValue = CommDefs.BATT_STATUS_NOT_CHARGING;
          break;
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
          mapValue = CommDefs.BATT_STATUS_UNKNOWN;
          break;
        }
      }
      if (mapKey != null) {
        try {
          GApp.infoJSON.put(mapKey, mapValue);
        } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } 
      }
    }
  }
  


  private HashMap<String, Object> callstateInfoMap(Bundle _extras) {
    HashMap<String, Object> info = Utils.toHashMap(_extras);
    /*String number = (String) info.get(ARCUtils.EXTRA_CALL_NUMBER);
    if (number != null && number.length()>1) {
      Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
          Uri.encode(number));
      Cursor cursor = getContentResolver().query(lookupUri,
          new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID }, null,
          null,
          null);
      if (cursor.moveToFirst()) {
        info.put(ARCUtils.EXTRA_CALL_NAME, cursor.getString(cursor
            .getColumnIndex(PhoneLookup.DISPLAY_NAME)));
        System.out.println(cursor.getString(cursor
            .getColumnIndex(PhoneLookup.DISPLAY_NAME)));
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
            cursor.getLong(cursor.getColumnIndex(PhoneLookup._ID)));
        info.put(ARCUtils.EXTRA_CALL_URI, contactUri.toString());
        InputStream photo=Contacts.openContactPhotoInputStream(getContentResolver(), contactUri);
        info.put(ARCUtils.EXTRA_CALL_PHOTO, ARCUtils.streamTobyteArray(photo));
      }
    }*/
    return info;
  }
}
